<template>
	<xn-form-container
		:title="formData.id ? '编辑互助金成员信息记录表' : '互助金成员申请表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="姓名：" name="memberName">
				<a-input v-model:value="formData.memberName" placeholder="请输入互助金成员姓名" allow-clear />
			</a-form-item>
			<a-form-item label="年龄：" name="memberAge">
				<a-input v-model:value="formData.memberAge" placeholder="请输入互助金成员年龄" allow-clear />
			</a-form-item>
			<a-form-item label="性别：" name="memberGen">
				<a-select v-model:value="formData.memberGen" placeholder="请选择互助金成员性别" :options="memberGenOptions" />
			</a-form-item>
			<a-form-item label="身份证号：" name="memberIdcard">
				<a-input v-model:value="formData.memberIdcard" placeholder="请输入互助金成员身份证号" allow-clear />
			</a-form-item>
			<a-form-item label="职工工作情况：" name="memberWork">
				<a-select
					v-model:value="formData.memberWork"
					:options="workStatusOptions"
					class="xn-wd"
					placeholder="请选择职工工作情况"
				/>
			</a-form-item>
			<a-form-item label="入职/退休时间：" name="memberWorkTime">
				<a-date-picker
					v-model:value="formData.memberWorkTime"
					value-format="YYYY-MM-DD HH:mm:ss"
					show-time
					placeholder="请选择入职/退休时间"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="选择工作单位：" name="memberOrg">
				<a-select v-model:value="formData.memberOrg" placeholder="请选择工作单位" :options="memberOrgOptions" />
			</a-form-item>
			<a-form-item label="上传申请/放弃加入互助金文件 (请上传pdf格式文件):" name="applyFile">
				<a-upload
					list-type="picture"
					v-model:file-list="fileList"
					:remove="handleRemove"
					accept=".pdf"
					:before-upload="beforeUpload"
					class="upload-list-inline"
				>
					<a-button>
						<upload-outlined></upload-outlined>
						选择上传文件
					</a-button>
				</a-upload>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizCooperationForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizCooperationApi from '@/api/biz/bizCooperationApi'
	import fileApi from '@/api/biz/bizFileManagerApi'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()

	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const memberGenOptions = ref([])
	const memberOrgOptions = ref([])
	const applyInfoOptions = ref([])
	const workStatusOptions = ref([])
	// 批量上传文件(文件接受队列)
	const fileList = ref([])

	// 文件处理（等选择后手动上传）
	const handleRemove = (file) => {
		const index = fileList.value.indexOf(file)
		const newFileList = fileList.value.slice()
		newFileList.splice(index, 1)
		fileList.value = newFileList
	}

	const beforeUpload = (file) => {
		fileList.value = [...fileList.value, file]
		return false
	}
	//上传文件的处理
	const uploadFile = async () => {
		const formData = new FormData()
		fileList.value.forEach((file) => {
			file = toRaw(file)
			formData.append('files', file.originFileObj)
		})
		return fileApi.fileUploadLocalReturnIdList(formData)
	}

	// 默认要校验的
	const formRules = {
		memberName: [required('请输入姓名')],
		memberAge: [required('请输入年龄')],
		memberGen: [required('请选择性别')],
		memberIdcard: [required('请输入身份证号')],
		memberWork: [required('请选择工作状态')],
		memberOrg: [required('请选择工作单位')],
		memberWorkTime: [required('请选择入职|退休时间')],
		orgName: [required('请选择组织')]
	}

	// 获取组织结构
	const getTree = () => {
		bizOrgApi.orgTree().then((res) => {
			let data = {
				value: res[0].id,
				label: res[0].name
			}
			memberOrgOptions.value.push(data)
			res[0].children.forEach((item) => {
				let data = {
					value: item.id,
					label: item.name
				}
				memberOrgOptions.value.push(data)
			})
		})
	}

	onMounted(() => {
		getTree()
	})

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		memberGenOptions.value = tool.dictList('GENDER')
		applyInfoOptions.value = tool.dictList('YSEORNO')
		workStatusOptions.value = tool.dictList('WORK_STATUS')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		fileList.value = []
		open.value = false
	}

	// 验证并提交数据
	const onSubmit = async () => {
		submitLoading.value = true
		const formDataParam = cloneDeep(formData.value)
		formDataParam.applyStatus = '10'
		if (formDataParam.applyFileUrl == null) {
			try {
				let res = await uploadFile()
				formDataParam.fileList = res
			} catch (error) {
				submitLoading.value = false
			}
		}
		bizCooperationApi
			.bizCooperationSubmitForm(formDataParam, formDataParam.id)
			.then(() => {
				onClose()
				emit('successful')
			})
			.finally(() => {
				submitLoading.value = false
			})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
